using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;

namespace SharePoint2010Poll
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("0299b250-46a0-49a6-8b98-d3135f5c3cc3")]
    public class ChartingWebConfigEventReceiver : SPFeatureReceiver
    {

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = null;
            try
            {
                webApp = properties.Feature.Parent as SPWebApplication;
                AddWebConfigSettings(webApp, properties.Feature.DefinitionId.ToString());
            }
            catch (Exception ex)
            {
                webApp.Features.Remove(properties.Feature.DefinitionId, true);
                throw new SPException("Error adding web.config properties for reporting, activation aborted. " + ex.Message);
            }
            base.FeatureActivated(properties);
           
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = null;
            try
            {
                webApp = properties.Feature.Parent as SPWebApplication;
                RemoveWebConfigSettings(webApp, properties.Feature.DefinitionId.ToString());
            }
            catch (Exception ex)
            {
                webApp.Features.Remove(properties.Feature.DefinitionId, true);
                throw new SPException("Error adding web.config properties for reporting, activation aborted. " + ex.Message);
            }
            base.FeatureDeactivating(properties);
        }

        public static void AddWebConfigSettings(SPWebApplication webApp, string owner)
        {
            //add to system.web/httpHandlers - for classic IIS web app's
            SPWebConfigModification httpHandlerElement = new SPWebConfigModification();
            httpHandlerElement.Name = "add[@path='ChartImg.axd'][@verb='GET,HEAD,POST'][@type='System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'][@validate='false']";
            httpHandlerElement.Path = "configuration/system.web/httpHandlers";
            httpHandlerElement.Sequence = 0;
            httpHandlerElement.Owner = owner;
            httpHandlerElement.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            httpHandlerElement.Value = "<add path=\"ChartImg.axd\" verb=\"GET,HEAD,POST\" type=\"System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35\" validate=\"false\" />";
            webApp.WebConfigModifications.Add(httpHandlerElement);

            //add configuration/system.web/compilation/assemblies
            SPWebConfigModification assemblyElement = new SPWebConfigModification();
            assemblyElement.Name = "add[@assembly='System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35']";
            assemblyElement.Path = "configuration/system.web/compilation/assemblies";
            assemblyElement.Sequence = 0;
            assemblyElement.Owner = owner;
            assemblyElement.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            assemblyElement.Value = "<add assembly=\"System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35\"/>";
            webApp.WebConfigModifications.Add(assemblyElement);

            //add to system.webServer/handlers - for native/integrated IIS web apps
            SPWebConfigModification handlersElement = new SPWebConfigModification();
            handlersElement.Name = "add[@name='ChartImg'][@verb='*'][@path='ChartImg.axd'][@type='System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35']";
            handlersElement.Path = "configuration/system.webServer/handlers";
            handlersElement.Sequence = 0;
            handlersElement.Owner = owner;
            handlersElement.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            handlersElement.Value = "<add name=\"ChartImg\" verb=\"*\" path=\"ChartImg.axd\"  type=\"System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35\"  />";
            webApp.WebConfigModifications.Add(handlersElement);

            //add temp image storage configuration configuration/appSettings    
            SPWebConfigModification imgStorage = new SPWebConfigModification();
            imgStorage.Name = "add[@key='ChartImageHandler'][@value='Storage=memory;Timeout=20;Url=~/_layouts/AUK.SharePoint.FTR/;']";
            imgStorage.Path = "configuration/appSettings";
            imgStorage.Sequence = 0;
            imgStorage.Owner = owner;
            imgStorage.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            imgStorage.Value = "<add key=\"ChartImageHandler\" value=\"Storage=memory;Timeout=20;Url=~/_layouts/AUK.SharePoint.FTR/;\" />";
            webApp.WebConfigModifications.Add(imgStorage);

            webApp.Update();
            webApp.WebService.ApplyWebConfigModifications();
        }

        public static void RemoveWebConfigSettings(SPWebApplication webApp, string owner)
        {
            //add to system.web/httpHandlers - for classic IIS web app's
            SPWebConfigModification httpHandlerElement = new SPWebConfigModification();
            httpHandlerElement.Name = "add[@path='ChartImg.axd'][@verb='GET,HEAD,POST'][@type='System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'][@validate='false']";
            httpHandlerElement.Path = "configuration/system.web/httpHandlers";
            httpHandlerElement.Sequence = 0;
            httpHandlerElement.Owner = owner;
            httpHandlerElement.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            httpHandlerElement.Value = "<add path=\"ChartImg.axd\" verb=\"GET,HEAD,POST\" type=\"System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35\" validate=\"false\" />";
            webApp.WebConfigModifications.Remove(httpHandlerElement);

            //add configuration/system.web/compilation/assemblies
            SPWebConfigModification assemblyElement = new SPWebConfigModification();
            assemblyElement.Name = "add[@assembly='System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35']";
            assemblyElement.Path = "configuration/system.web/compilation/assemblies";
            assemblyElement.Sequence = 0;
            assemblyElement.Owner = owner;
            assemblyElement.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            assemblyElement.Value = "<add assembly=\"System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35\"/>";
            webApp.WebConfigModifications.Remove(assemblyElement);

            //add to system.webServer/handlers - for native/integrated IIS web apps
            SPWebConfigModification handlersElement = new SPWebConfigModification();
            handlersElement.Name = "add[@name='ChartImg'][@verb='*'][@path='ChartImg.axd'][@type='System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35']";
            handlersElement.Path = "configuration/system.webServer/handlers";
            handlersElement.Sequence = 0;
            handlersElement.Owner = owner;
            handlersElement.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            handlersElement.Value = "<add name=\"ChartImg\" verb=\"*\" path=\"ChartImg.axd\"  type=\"System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35\"  />";
            webApp.WebConfigModifications.Remove(handlersElement);      

            //add temp image storage configuration configuration/appSettings    
            SPWebConfigModification imgStorage = new SPWebConfigModification();
            imgStorage.Name = "add[@key='ChartImageHandler'][@value='Storage=memory;Timeout=20;Url=~/_layouts/pollwebpart/;']";
            imgStorage.Path = "configuration/appSettings";
            imgStorage.Sequence = 0;
            imgStorage.Owner = owner;
            imgStorage.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            imgStorage.Value = "<add key=\"ChartImageHandler\" value=\"Storage=memory;Timeout=20;Url=~/_layouts/pollwebpart/;\" />";
            webApp.WebConfigModifications.Remove(imgStorage);

            webApp.Update();
            webApp.WebService.ApplyWebConfigModifications();
        }
    }
}
